package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user = new User();
        UserDao userDao = new UserDaoImpl();
        try{
            user = userDao.getUserByIdByPass(userId, password);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return user;
    }

    @Override
    public int getUserById(String userId) {
        int result = 0;
        try {
            DBUtil.beginTransaction();
            UserDaoImpl userDao = new UserDaoImpl();
            result = userDao.getUserById(userId);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int saveUser(String userId, String password, String userName, Integer userSex) {
        int result = 0;
        try {
            DBUtil.beginTransaction();
            UserDaoImpl userDao = new UserDaoImpl();
            result = userDao.saveUser(userId,password,userName,userSex);
            DBUtil.commitTransaction();

        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }


}
